package com.gzqylc.docker.extend.gateway.service;

import com.gzqylc.docker.extend.gateway.dao.GateWayConfigDao;
import com.gzqylc.docker.extend.gateway.domain.GateWayConfig;
import com.gzqylc.utils.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;

/**
 * @Desc：TODO
 * @ClassName GateWayConfigService
 * @Author 孔晓文
 * @Date 2022/2/16 下午3:26
 */
@Service
public class GateWayConfigService {


    @Autowired
    GateWayConfigDao gateWayConfigDao;

    public Page<GateWayConfig> page(GateWayConfig gatewayConfig, Pageable pageable){
        return gateWayConfigDao.findAll(gatewayConfig,pageable);
    }

    public void saveOrUpdate(GateWayConfig gatewayConfig){

        if (gatewayConfig.getUrl().endsWith("/")){
            Assert.isTrue(true,"url请勿用\"/\"结尾");
        }
        gateWayConfigDao.save(gatewayConfig);
    }

    public void delete(String id){
        gateWayConfigDao.deleteById(id);
    }

    public String getConfig(String url,String code){
        Map<String, String> headers = new HashMap<>();
        headers.put("simpleCode",code);
        try {
          return HttpUtils.httpGet(url,headers);
        }catch (Exception e) {
            throw new RuntimeException("请求错误");
        }
    }

    public void updateConfig(String url,String code,String data){
        Map<String, String> headers = new HashMap<>();
        headers.put("simpleCode",code);
        try {
            HttpUtils.httpPostJson(url,headers,data);
        }catch (Exception e) {
            throw new RuntimeException("请求错误");
        }
    }
}
